package com.woniuxy.controller;

import com.woniuxy.dao.PeccancyRecordMapper;
import com.woniuxy.pojo.Car;
import com.woniuxy.pojo.Dictionary;
import com.woniuxy.pojo.Driver;
import com.woniuxy.pojo.PeccancyRecord;
import com.woniuxy.service.PeccancyRecordService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.PeccancyRecordVo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PeccancyRecordController extends BaseController{
    private Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PeccancyRecordService peccancyRecordService;

    @ApiOperation(value = "查找违章记录",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "查找违章记录")
    @GetMapping("/peccancyRecords")
    public DataGridView findPeccancyRecordsByPage(PeccancyRecordVo peccancyRecordVo){
        logger.debug("违章记录："+peccancyRecordVo.toString());
        return peccancyRecordService.findByPage(peccancyRecordVo);
    }

    @ApiOperation(value = "删除违章记录",httpMethod = "DELETE",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "删除违章记录")
    @DeleteMapping(value = "/peccancyRecords/{id}")
    public ResultDto removePeccancyRecord(@PathVariable("id") int id){
        try {
            peccancyRecordService.removeById(id);
            return ResultDto.PECCANCY_RECORD_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.PECCANCY_RECORD_DELETE_SUCCESS;
        }
    }

    @ApiOperation(value = "批量删除违章记录",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "批量删除违章记录")
    @GetMapping(value = "peccancyRecords/deleteBatch")
    public ResultDto removeBathPeccancyRecords(@RequestParam("ids[]") Integer[] ids){
        try {
            peccancyRecordService.removeBatch(ids);
            return ResultDto.PECCANCY_RECORD_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.PECCANCY_RECORD_DELETE_SUCCESS;
        }
    }

    @ApiOperation(value = "添加违章记录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "添加违章记录")
    @PostMapping(value = "/peccancyRecords")
    public ResultDto addPeccancyRecord(PeccancyRecord peccancyRecord){
        try {
            boolean flag=peccancyRecordService.add(peccancyRecord);
            if (flag) {
                return ResultDto.PECCANCY_RECORD_ADD_SUCCESS;
            } else {
                return ResultDto.PECCANCY_RECORD_ADD_FAIL;
            }
        } catch (Exception e){
            e.printStackTrace();
            return ResultDto.PECCANCY_RECORD_ADD_FAIL;
        }
    }

    @ApiOperation(value = "编辑违章记录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "编辑违章记录")
    @PostMapping(value = "/peccancyRecords.do")
    public ResultDto updatePeccancyRecord(PeccancyRecord peccancyRecord){
        try {
            boolean flag=peccancyRecordService.update(peccancyRecord);
            if (flag) {
                return ResultDto.PECCANCY_RECORD_UPDATE_SUCCESS;
            } else {
                return ResultDto.PECCANCY_RECORD_UPDATE_FAIL;
            }
        } catch (Exception e){
            e.printStackTrace();
            return ResultDto.PECCANCY_RECORD_UPDATE_FAIL;
        }
    }

    @Autowired
    PeccancyRecordMapper peccancyRecordMapper;

    @GetMapping("/pec/cars")
    public List<Car> cars(){
        return peccancyRecordMapper.selectAllCars();
    }

    @GetMapping("/pec/drivers")
    public List<Driver> drivers(){
        return peccancyRecordMapper.selectAllDrivers();
    }

    @GetMapping("/pec/options")
    public List<Dictionary> options(){
        return peccancyRecordMapper.selectAllOptions();
    }
}
